<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<head>
<title>客户编辑</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="stylesheet" href="./css/font.css">
<link rel="stylesheet" href="./css/xadmin.css">
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="./js/xadmin.js"></script>
<script type="text/javascript" src="js/address.js"></script>
<script type="text/javascript" src="./js/cookie.js"></script>
</head>

<body>
<script>
    $(function () {
        var url = location.search;
        console.log(url);
        var id=url.split("=")[1];
        var str= address + "customer/get/" + id;
        $.ajax({
            url:str,
            type:"GET",
            dataType:'JSON',
            contentType : "application/json; charset=utf-8",
            success:function(data){
                document.getElementById("L_name").value=data.customer.name;
                document.getElementById("L_telCode").value=data.customer.telCode;
                document.getElementById("L_department").value=data.customer.department;
                document.getElementById("L_regionCode").value=data.customer.regionCode;
                document.getElementById("L_address").value=data.customer.address;
                document.getElementById("L_postCode").value=data.customer.postCode;
            },
            error:function () {
                alert("error");
            }
        });
        $("#edit").click(function () {
            var name = $("#L_name").val();
            var telcode = $("#L_telCode").val();
            var department = $("#L_department").val();
            var regioncode = $("#L_regionCode").val();
            var myAddress = $("#L_address").val();
            var postcode = $("#L_postCode").val();

            $.ajax({
                url: address + "customer/update" ,
                type: "post",
                dataType: "JSON",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({
                    "id":id, "name": name, "telCode": telcode, "department": department,
                    "regionCode": regioncode, "address": myAddress, "postCode": postcode
                }),
                success: function (data) {
                    layer.msg('已更新!', {icon: 1, time: 1000});
                    x_admin_father_reload();
                    x_admin_close();
                    open(location, '_self').close();
                },
                error: function (jqXHR) {
                    layer.msg(jqXHR.responseText);
                }
            });
        });
    });

</script>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="L_name" class="layui-form-label">
                <span class="x-red">*</span>姓名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_name" value="" name="name" required="" lay-verify="name" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_telCode" class="layui-form-label">
                <span class="x-red">*</span>电话
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_telCode" name="telCode" required="" lay-verify="telCode" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_department" class="layui-form-label">
                <span class="x-red">*</span>工作单位
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_department" name="department" required="department" lay-verify="department" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_regionCode" class="layui-form-label">
                <span class="x-red">*</span>地区编码
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_regionCode" name="regionCode" required="regionCode" lay-verify="regionCode" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_address" class="layui-form-label">
                <span class="x-red">*</span>住址
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_address" name="address" required="address" lay-verify="address" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_postCode" class="layui-form-label">
                <span class="x-red">*</span>邮政编码
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_postCode" name="postCode" required="postCode" lay-verify="postCode" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button id="edit"  class="layui-btn" lay-filter="edit" lay-submit="">修改</button>
        </div>
    </form>
</div>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            name: function(value){
                if(value === '' || value === null){
                    return '姓名不能为空';
                }
            }
            ,telCode: function(value){
                if(value === '' || value === null){
                    return '电话不能为空';
                }
            }
            ,department: function(value){
                if(value === '' || value === null){
                    return '工作单位不能为空';
                }
            }
            ,regionCode: function(value){
                if(value === '' || value === null){
                    return '地区不能为空';
                }
            }
            ,address: function(value){
                if(value === '' || value === null){
                    return '地址不能为空';
                }
            }
            ,postCode: function(value){
                if(value === '' || value === null){
                    return '邮政编码不能为空';
                }
            }
        });
        //监听提交
        form.on('submit(edit)', function(){

            layer.alert("修改成功", {icon: 6},function () {
                //关闭当前frame
                x_admin_close();
                // 可以对父窗口进行刷新
                x_admin_father_reload();
            });
            return false;
        });
    });
</script>
</body>
</html>